<?php
require_once 'lib/controllerWithoutGetOrPost.php';
function initFlowers($personId)
{
    global $db;
    $sql = <<< EOT
        INSERT INTO
            RemembranceFlowers(PersonID, Count)
        VALUES
            (%d, 1)
EOT;
    $sql = sprintf($sql, $personId);
    $result = $db->query($sql);
    return $result === TRUE;
}
function increaseFlowers($personId)
{
    global $db;
    $sql = <<< EOT
        UPDATE RemembranceFlowers
        SET Count = Count + 1
        WHERE PersonID = %d
EOT;
    $sql = sprintf($sql, $personId);
    $result = $db->query($sql);
    if ($result === FALSE) {
        $result = initFlowers($personId);
    }
    return $result === TRUE;
}
function getFlowersCount($personId)
{
    global $db;
    $count = 0;
    $sql = <<< EOT
        SELECT Count
        FROM RemembranceFlowers
        WHERE PersonID = %d
EOT;
    $sql = sprintf($sql, $personId);
    $result = $db->query($sql);
    if (!empty($result)) {
        $row = $db->fetch($result);
        $count = $row['Count'];
    }
    return $count;
}
$personId = intval($_GET['personid']);
increaseFlowers($personId);
echo getFlowersCount($personId);
?>